package android.pot.com.smartcar.ui.login;

import android.os.Build;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.CommonListner;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.entity.UserInfo;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.LoginTool;
import android.pot.com.smartcar.tool.TipToast;
import android.pot.com.smartcar.ui.register.RegistersActivity;
import android.pot.com.smartcar.util.GsonUtil;
import android.pot.com.smartcar.util.OSUtils;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhaoshilu on 2017/11/29.
 */
public class LoginsActivity extends SwipeBackActivity implements View.OnClickListener{


    private EditText et_zhanghao;

    private EditText et_psd;

    private Map<String, String> params;

    private final static int MIUI = 0;

    private final static int FLYME = 1;

    private final static int COMMON = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logins);
        setImmersiveStatusBar(true);
        initView();
        addListener();
        initData();

    }

    protected void initView() {


        et_psd = (EditText) findViewById(R.id.et_psd);
        et_zhanghao = (EditText) findViewById(R.id.et_zhanghao);
        UserInfo userInfo = UserInfo.instance();
        et_zhanghao.setText(UserInfo.instance().getPhone());

        et_zhanghao.setSelection(et_zhanghao.getText().length());


    }

    protected void addListener() {
        findViewById(R.id.bt_login).setOnClickListener(this);

        findViewById(R.id.tv_register).setOnClickListener(this);

        findViewById(R.id.tv_findpsd).setOnClickListener(this);


    }

    protected void initData() {


    }

    protected void setImmersiveStatusBar(boolean fontIconDark) {
        if (fontIconDark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                setStatusBarFontIconDark(COMMON);
            } else if (OSUtils.isMiui()) {
                setStatusBarFontIconDark(MIUI);
            } else if (OSUtils.isFlyme()) {
                setStatusBarFontIconDark(FLYME);
            } else {//其他情况下我们将状态栏设置为灰色，就不会看不见字体
//                setStatusBarPlaceColor(Color.LTGRAY);//灰色
            }
        }
    }

    /**
     * 设置文字颜色
     */
    public void setStatusBarFontIconDark(int type) {
        switch (type) {
            case MIUI:
                setMiuiUI(true);
                break;
            case COMMON:
                setCommonUI();
                break;
            case FLYME:
                setFlymeUI(true);
                break;
        }
    }
    public void setCommonUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        }
    }

    public void setFlymeUI(boolean dark) {
        try {
            Window window = getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (dark) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMiuiUI(boolean dark) {
        try {
            Window window = getWindow();
            Class clazz = getWindow().getClass();
            Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            int darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            if (dark) {    //状态栏亮色且黑色字体
                extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
            } else {
                extraFlagField.invoke(window, 0, darkModeFlag);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void login(){
        params = new HashMap<String, String>();
        if (TextUtils.isEmpty(et_zhanghao.getText().toString())) {
            TipToast.show( getString(R.string.input_zhanghao));
            return;
        }
        if (TextUtils.isEmpty(et_psd.getText().toString())){
            TipToast.show( getString(R.string.input_password));
            return;
        }else {
                if (et_psd.getText().toString().trim().length()<6 || et_psd.getText().toString().trim().length()>12) {
                    TipToast.show("请输入6-12密码");
                    return;
                }
        }

        params.put("phone", et_zhanghao.getText().toString());
        params.put("password", et_psd.getText().toString());
        LoadingTool.StartLoading(this);
        Action.get().Login(LoginsActivity.this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                LoadingTool.EndLoading(LoginsActivity.this);
                final UserInfo info =  GsonUtil.getObject(response.toString(),UserInfo.class);
                LoginTool.setLoginIn(LoginsActivity.this,info);


                scrollToFinishActivity();
                if (null!= getIntent().getExtras()) {
                    if (getIntent().getStringExtra("from").equals("mine")){
                        CommonListner.instance().getLoginInListener().ready();
                    }

                }

            }
        });

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
                login();

                break;
            case R.id.tv_findpsd:
                startAct(LoginsActivity.this, FindLoginsPsdActivity.class);
                break;
            case R.id.tv_register:
                startAct(LoginsActivity.this, RegistersActivity.class);
                break;

        }
    }
}